package com.siriframe.security.service.impl;

import com.siriframe.entity.Result;
import com.siriframe.security.dao.MenuMapper;
import com.siriframe.security.dao.UserMapper;
import com.siriframe.security.dao.UserRoleMapper;
import com.siriframe.security.entity.dto.UserRegisterDTO;
import com.siriframe.security.entity.po.User;
import com.siriframe.security.entity.po.UserRole;
import com.siriframe.security.enums.Role;
import com.siriframe.security.service.UserRegisterService;
import com.siriframe.security.util.UserCheckUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Date;

@Service
public class UserRegisterServiceImpl implements UserRegisterService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private MenuMapper menuMapper;

    @Override
    @Transactional
    public Result userRegister(UserRegisterDTO userRegisterDTO) {
        User user = new User();
        user.setUsername(userRegisterDTO.getUsername());
        //检查用户名是否已经被使用过
        boolean flag = UserCheckUtils.checkUsernameExist(userRegisterDTO.getUsername());

        if(flag){
            user.setIdentityCard(userRegisterDTO.getIdentityCard());//身份证是否需要不重复？-> 一个人多个账号
            user.setPassword(userRegisterDTO.getPassword());
            user.setGender(userRegisterDTO.getGender());
            user.setLocation(userRegisterDTO.getLocation());
            user.setUserImage(userRegisterDTO.getUserImage());
            user.setPhoneNumber(userRegisterDTO.getPhoneNumber());
            user.setCreateTime(new Date((new java.util.Date().getTime())).toString());
            user.setUpdateTime(new Date((new java.util.Date().getTime())).toString());
            user.setIsDelete(0);

            userMapper.insert(user);
            Long userId = user.getId();

            if(Role.USER_COMMON.getRoleName().equals(userRegisterDTO.getRole())){
                UserRole userRole = new UserRole();
                userRole.setUserId(userId);
                userRole.setRoleId(Role.USER_COMMON.getRoleId());
                userRoleMapper.insert(userRole);
            }
            else if(Role.USER_PROVIDER.getRoleName().equals(userRegisterDTO.getRole())){
                UserRole userRole = new UserRole();
                userRole.setUserId(userId);
                userRole.setRoleId(Role.USER_PROVIDER.getRoleId());
                userRoleMapper.insert(userRole);
            }else {
                return Result.error("没有role这个角色");
            }
            return Result.ok("注册成功");
        }else {
            return Result.error("该用户名已创建");
        }
    }

    @Override
    @Transactional
    public Result userSystemRegister(UserRegisterDTO userRegisterDTO) {
        User user = new User();
        user.setUsername(userRegisterDTO.getUsername());
        //检查用户名是否已经被使用过
        boolean flag = UserCheckUtils.checkUsernameExist(userRegisterDTO.getUsername());

        if(flag){
            user.setIdentityCard(userRegisterDTO.getIdentityCard());
            user.setPassword(userRegisterDTO.getPassword());
            user.setGender(userRegisterDTO.getGender());
            user.setLocation(userRegisterDTO.getLocation());
            user.setUserImage(userRegisterDTO.getUserImage());
            user.setPhoneNumber(userRegisterDTO.getPhoneNumber());
            user.setCreateTime(new Date((new java.util.Date().getTime())).toString());
            user.setUpdateTime(new Date((new java.util.Date().getTime())).toString());
            user.setIsDelete(0);

            userMapper.insert(user);
            Long userId = user.getId();

            UserRole userRole = new UserRole();
            userRole.setUserId(userId);
            userRole.setRoleId(Role.SYSTEM_ADMINISTRATOR.getRoleId());
            userRoleMapper.insert(userRole);

            return Result.ok(200,"注册成功");
        }else {
            return Result.error("该用户名已创建");
        }
    }
}
